package com.sch.www.sys.services.impl;

import com.alibaba.fastjson.JSONObject;
import com.sch.www.common.base.BaseServicesSimpleImp;
import com.sch.www.sys.orm.dao.manual.AccountToRoleMapper;
import com.sch.www.sys.orm.entity.manual.AccountToRole;
import com.sch.www.sys.orm.entity.manual. AccountToRoleExample;
import com.sch.www.sys.services.RoleServices;
import com.sch.www.sys.services.AccountToRoleServices;
import com.sch.www.sys.services.AccountServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AccountToRoleServicesImpl extends BaseServicesSimpleImp<AccountToRole,  AccountToRoleExample, AccountToRoleMapper, AccountToRoleServices> implements AccountToRoleServices {
    @Autowired
    AccountServices accountServices;
    @Autowired
    RoleServices roleServices;

    @Override
    protected String getTableName() {
        return "";
    }

    @Override
    protected  AccountToRoleExample getExampleNewInstance() {
        return new  AccountToRoleExample();
    }

    @Override
    public List<String> selectRoleIdByAccountId(String... accountIds) {
        if (isNull(accountIds)) {
            return new ArrayList<>();
        } else {
             AccountToRoleExample example = getExampleNewInstance();
            if (accountIds.length == 1) {
                example.createCriteria().andAccountIdEqualTo(accountIds[0]);
            } else {
                example.createCriteria().andAccountIdIn(Arrays.asList(accountIds));
            }
            List<AccountToRole> accountLinkRoleList = getMapper().selectByExample(example);
            List<String> roleIdList = accountLinkRoleList.stream().map(item -> item.getRoleId()).collect(Collectors.toList());
            return roleIdList;
        }
    }

    @Override
    public List<String> selectAccountIdByRoleId(String... roleIds) {
        if (isNull(roleIds)) {
            return new ArrayList<>();
        } else {
             AccountToRoleExample example = getExampleNewInstance();
            if (roleIds.length == 1) {
                example.createCriteria().andRoleIdEqualTo(roleIds[0]);
            } else {
                example.createCriteria().andRoleIdIn(Arrays.asList(roleIds));
            }
            List<AccountToRole> accountLinkRoleList = getMapper().selectByExample(example);
            List<String> accountIdList = accountLinkRoleList.stream().map(item -> item.getAccountId()).collect(Collectors.toList());
            return accountIdList;
        }
    }
    


    @Transactional
    @Override
    public void insert(String accountId, List<String> roleIdList) {

        if (accountServices.selectById(accountId) == null) {
            throw new IllegalArgumentException("不存accountId为" + accountId + "的用户");
        }

        if (CollectionUtils.isEmpty(roleIdList) || roleIdList.size() != roleServices.selectById(roleIdList).size()) {
            throw new IllegalArgumentException("roleIdList异常：" + JSONObject.toJSONString(roleIdList));
        }

        List<AccountToRole> list = new ArrayList<>();
        roleIdList.forEach(roleId -> {
            AccountToRole accountLinkRole = new AccountToRole();
            accountLinkRole.setAccountId(accountId);
            accountLinkRole.setRoleId(roleId);
            list.add(accountLinkRole);
        });
        getCurrentProxy().insertSelective(list.toArray(new AccountToRole[]{}));


    }
}
